<?php
	/**
	* 
	*/
	class FanliinterfaceAction extends Action
	{		
		public function index(){
			$condition=[];
			//搜索
			if(isset($_REQUEST['search_content'])){
				$condition['fanliinterface_name']=array('LIKE','%'.$_REQUEST['search_content'].'%');
				$condition['fanliinterface_url']=array('LIKE','%'.$_REQUEST['search_content'].'%');
				$condition['fanliinterface_comments']=array('LIKE','%'.$_REQUEST['search_content'].'%');
				$condition['_logic'] = 'OR';
			}
			$size=10;
			// 总条数
			$total_counts=D('Fanliinterface')->getCounts($condition);
			//总页数
			$total_pages=ceil($total_counts/$size);
			$current_p=isset($_REQUEST['p'])?$_REQUEST['p']:1;
			$start=($current_p-1)*$size;
			$previous_p=1;
			$previous_disable=true;
			if($current_p>1){
				$previous_p=$current_p-1;
				$previous_disable=false;
			}
			$next_p=$total_pages;
			$next_disable=true;
			if($current_p<$total_pages){
				$next_p=$current_p+1;
				$next_disable=false;
			}			

			$interfaces=D('Fanliinterface')->getInterfaces($condition,$start,$size);
			$this->assign('total_counts',$total_counts);
			$this->assign('total_pages',$total_pages);
			$this->assign('current_p',$current_p);
			$this->assign('previous_p',$previous_p);
			$this->assign('previous_disable',$previous_disable);
			$this->assign('next_p',$next_p);
			$this->assign('next_disable',$next_disable);
			$this->assign('interfaces',$interfaces);
			$this->display('Fanliinterface/index');
		}


		public function ajaxDoAdd(){
			$result=array(
				'status'=>0,
				'data'=>'数据缺失',
				);

			$data=[];

			//数据检查
			if(!isset($_REQUEST['fanliinterface_name'])|| empty($_REQUEST['fanliinterface_name'])||!isset($_REQUEST['fanliinterface_url'])|| empty($_REQUEST['fanliinterface_url'])){
				$this->ajaxReturn($result);
			}else{
				$data['fanliinterface_name']=$_REQUEST['fanliinterface_name'];
				$data['fanliinterface_url']=$_REQUEST['fanliinterface_url'];
				$data['fanliinterface_comments']=isset($_REQUEST['fanliinterface_comments'])?$_REQUEST['fanliinterface_comments']:'';
				$data['fanliinterface_add_time']=date('Y-m-d h:i:sa');
			}

			//重复
			$condition['fanliinterface_url']=array('LIKE','%'.$_REQUEST['fanliinterface_url'].'%');
			if(D('Fanliinterface')->getInterfaces($condition,0,1)){
				$result['status']=0;
				$result['data']='数据重复';
				$this->ajaxReturn($result);
			}

			if(!D('Fanliinterface')->addInterface($data)){
				$result['status']=0;
				$result['data']='添加失败';
				$this->ajaxReturn($result);
			}

			$result['status']=1;
			$result['data']='添加成功';
			$this->ajaxReturn($result);
		}

		public function ajaxDoDel(){
			$result=array(
				'status'=>0,
				'data'=>'数据缺失'
				);
			$condition=[];

			if(!isset($_REQUEST['id'])||empty($_REQUEST['id'])){
				$this->ajaxReturn($result);
			}else{
				$condition['id']=$_REQUEST['id'];
			}

			if(!D('Fanliinterface')->delInterface($condition)){
				$result['status']=0;
				$result['data']='删除失败';
				$this->ajaxReturn($result);
			}

			$result['status']=1;
			$result['data']='删除成功';
			$this->ajaxReturn($result);
		}
	}